//
//  iMB_CameraAnimation.m
//  TestCameraAnimation
//
//  Created by 刘泽琛 on 14-7-30.
//  Copyright (c) 2014年 刘 泽琛. All rights reserved.
//

#import "iMB_CameraAnimation.h"
#define UP_DOWN_SPACE 40

@implementation iMB_CameraAnimation

- (void)initWithCameraAnimation {
    [self joiningTogatherUI];
}

- (void)joiningTogatherUI {
    // 上部图片视图
    if (!_upImageView) {
        _upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/2)];
        [_upImageView setImage:[UIImage imageNamed:@"realcamera_movieup"]];
        [self addSubview:_upImageView];
    }
    
    // 下部图片视图
    if (!_downImageView) {
        _downImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.frame.size.height/2-UP_DOWN_SPACE, self.frame.size.width, self.frame.size.height/2+UP_DOWN_SPACE)];
        [_downImageView setImage:[[UIImage imageWithCGImage:[UIImage imageNamed:@"realcamera_moviedown"].CGImage scale:2.0 orientation:UIImageOrientationUp] resizableImageWithCapInsets:UIEdgeInsetsMake(100, 0, 0, 0)]];
        [self addSubview:_downImageView];
    }
    [self performSelector:@selector(viewToOpen) withObject:nil afterDelay:1];
}

#pragma mark - 开启
- (void)viewToOpen {
    [self upImageViewToUP];
    [self downImageViewToDown];
}

- (void)upImageViewToUP {
    [UIView animateWithDuration:0.2 animations:^{
        _upImageView.frame = CGRectMake(0, 0, self.frame.size.width, 0);
    } completion:^(BOOL finished) {
    }];
}

- (void)downImageViewToDown {
    [UIView animateWithDuration:0.2 animations:^{
        _downImageView.frame = CGRectMake(0, CGRectGetMaxX(_downImageView.frame), self.frame.size.width, 0);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - 关闭
- (void)viewToClose {
    [self upImageViewToDown];
    [self downImageViewToUP];
    
    [self performSelector:@selector(viewToOpen) withObject:nil afterDelay:1];
}

- (void)upImageViewToDown {
    [UIView animateWithDuration:0.2 animations:^{
        _upImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/2);
    } completion:^(BOOL finished) {
        // code...
    }];
}

- (void)downImageViewToUP {
    [UIView animateWithDuration:0.2 animations:^{
        _downImageView.frame = CGRectMake(0, self.frame.size.height/2-UP_DOWN_SPACE, self.frame.size.width, self.frame.size.height/2+UP_DOWN_SPACE);
    } completion:^(BOOL finished) {
        // code...
    }];
}

- (void)dealloc
{
    NSLog(@"相机动画视图 释放了...");
}

@end
